#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define D long double
#define N 4000006
//#define int long long 
template <class T = int>
inline T read() {
    char c = getchar();
    bool sign = (c == '-');
    while (c < '0' || c > '9')
        c = getchar(), sign |= (c == '-');
    T x = 0;
    while ('0' <= c && c <= '9')
        x = (x << 1) + (x << 3) + (c & 15), c = getchar();
    return sign ? (~x + 1) : x;
}
struct node{
	int x,v;
}a[N];
bool cmp(node a,node b){
	return a.v>b.v;
}
vector<int> val[1000006];int cnt,pos[1000006];
signed main(){
	freopen("contest.in","r",stdin);
	freopen("contest.out","w",stdout);
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int n;cin>>n;//n=read();
	for(int i=1;i<=n;i++){
		int k;cin>>k;//k=read();
		for(int j=1;j<=k;j++){
			int x;cin>>x;/*x=read();*/val[i].push_back(x);
			cnt++;a[cnt]={i,x};
		}
		sort(val[i].begin(),val[i].end(),greater<int>());
	}
	sort(a+1,a+cnt+1,cmp);
	int mn=2e9;
	for(int i=1;i<=n;i++)	mn=min(mn,val[i][0]);
	int ans=a[1].v-mn;
	for(int i=2;i<=cnt;i++){
		int lst=a[i-1].x;
		if(pos[lst]==(val[lst].size()-1))	break;
		pos[lst]++;mn=min(mn,val[lst][pos[lst]]);
		ans=min(ans,a[i].v-mn);
	}
	cout<<ans;
	return 0;
}
/*#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define D long double
#define N 4000006
#define int long long 
template <class T = int>
inline T read() {
    char c = getchar();
    bool sign = (c == '-');
    while (c < '0' || c > '9')
        c = getchar(), sign |= (c == '-');
    T x = 0;
    while ('0' <= c && c <= '9')
        x = (x << 1) + (x << 3) + (c & 15), c = getchar();
    return sign ? (~x + 1) : x;
}
vector<int> val[1000006];int a[N],cnt,pos[1000006];
struct node{
	int val,x;
	bool operator<(const node &a)const{
		return val<a.val;
	}
};
priority_queue<node> q;
signed main(){
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int n;n=read();
	for(int i=1;i<=n;i++){
		int k;k=read();
		for(int j=1;j<=k;j++){
			int x;x=read();val[i].push_back(x);
			cnt++;a[cnt]=x;
		}
		sort(val[i].begin(),val[i].end(),greater<int>());
	}
	sort(a+1,a+cnt+1,greater<int>());
	int mn=1e16;
	for(int i=1;i<=n;i++)	q.push({val[i][0],i}),mn=min(mn,val[i][0]);
	int ans=1e16;
	for(int i=1;i<=cnt;i++){
		
	}
	return 0;
}*/
